package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.view.CustomChooseView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by zw on 2016/3/1 0001.
 * 是否
 */
public class TrueFalseActivity extends ABBaseActivity implements View.OnClickListener {

    private TitleBar titleBar;
    private LinearLayout layout_content;
    private RelativeLayout rl_all;
    private ImageView iv_all;

    private boolean isAll = false;//是否全选

    private String intentFlag;

    private Intent intent;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_type;
    }

    @Override
    protected void initWidget() {
        titleBar = getViewById(R.id.title_bar);
        titleBar.setShowRight(View.VISIBLE);
        titleBar.setRightTextView("确定");
        titleBar.setRightTextColor(R.color.unify_grounding_white);

        layout_content = getViewById(R.id.layout_content);
        rl_all = getViewById(R.id.layout_all);
        iv_all = getViewById(R.id.iv_all);

        if(bundle != null){
            intentFlag = bundle.getString(Constant.INTENT_FLAG);
        }
        titleBar.setTiteTextView("包含下级");

        initData();

    }

    private void initData(){
        String[] strs = {"是", "否"};
        for(int i = 0; i < strs.length; i ++){
            CustomChooseView chooseView = new CustomChooseView(mContext);
            chooseView.setTitle(strs[i]);
            chooseView.setTag(i);
            chooseView.setOnClickListener(this);
            layout_content.addView(chooseView);
        }
    }

    @Override
    protected void eventOnClick() {
        rl_all.setOnClickListener(this);

        titleBar.setRightOnClickListener(new TitleBar.RightBtnOnClickListener() {
            @Override
            public void onRightClick(View view) {
                intent = new Intent();
                if(isAll){
                    intent.putExtra("tag", "全部");
                }else {
                    for (int i = 0; i < layout_content.getChildCount(); i++) {
                        CustomChooseView cv = (CustomChooseView) layout_content.getChildAt(i);
                        if (cv.getIsSelected()) {
                            intent.putExtra("tag", cv.getLeftTitle());
                        }
                    }
                }
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_all://全选
                changeState();
                break;
            default:
                if(v.getTag() != null){
                    int index = (int) v.getTag();
                    singleSelect(index);
                }
                break;
        }
    }

    /**
     * 单选
     * @param index
     */
    private void singleSelect(int index){
        for (int i = 0; i < layout_content.getChildCount(); i++){
            CustomChooseView childView = (CustomChooseView) layout_content.getChildAt(i);
            if(i == index){
                childView.setIsSelected(true);
            }else{
                childView.setIsSelected(false);
            }
        }

    }

    /**
     * 选中全部的时候改变其他状态为不选中
     */
    private void changeState(){
        isAll = !isAll;
        if(isAll){
            iv_all.setVisibility(View.VISIBLE);
            for (int i = 0; i < layout_content.getChildCount(); i++){
                CustomChooseView childView = (CustomChooseView) layout_content.getChildAt(i);
                childView.setIsSelected(false);
                childView.setCanChoose(false);
            }
        }else{
            iv_all.setVisibility(View.GONE);
            for (int i = 0; i < layout_content.getChildCount(); i++){
                CustomChooseView childView = (CustomChooseView) layout_content.getChildAt(i);
                childView.setCanChoose(true);
            }
        }

    }
}
